﻿Imports System.Collections.Specialized
Imports System.Windows.Forms

Public Class LendAdd1

#Region "插入模式"

    Public Overrides Function DoAddCheck() As Boolean
        DoAddCheck = False

        If Me.txtBorrowUserName.Text.Trim = String.Empty Then
            ShowMessageBox("借款编号不能为空")
            Me.btnLendUsername.Focus()
            Exit Function
        End If

        If NumericUpDown1.Value <= 0 Then
            ShowMessageBox("此借款已经还完")
            Me.NumericUpDown1.Focus()
            Exit Function
        End If

        If numUpDown.Value <= 0 Then
            ShowMessageBox("还款不能小于0")
            Me.numUpDown.Focus()
            Exit Function
        End If

        If numUpDown.Value > Me.NumericUpDown1.Value Then
            If ShowMessageResult("还款大于借款余额,是否继续？") = Windows.Forms.DialogResult.No Then
                Me.numUpDown.Focus()
                Exit Function
            End If
        End If

        'If Me.chk是否启用关联更新.Checked Then
        '    If numUpDown.Value <> Me.NM支出关联金额1.Value + Me.NM支出关联金额2.Value Then
        '        ShowMessageBox("关联金额必须与还款金额相等")
        '        Me.NM支出关联金额1.Focus()
        '        Exit Function
        '    End If
        'End If

        If chk是否启用关联更新.Checked Then
            If numUpDown.Value <> Me.NM支出关联金额1.Value + Me.NM支出关联金额2.Value Then
                If ShowMessageResult("关联金额与还款金额不相等,是否继续？") = Windows.Forms.DialogResult.No Then
                    Me.NM支出关联金额1.Focus()
                    Exit Function
                End If
            End If
        End If

        If Me.txtUsername.Text.Trim = String.Empty Then
            ShowMessageBox("操作用户不能为空")
            Me.btnUsername.Focus()
            Exit Function
        End If

        DoAddCheck = True
    End Function

    Public Overrides Function DoAddExist() As Boolean
        Return True
    End Function

    Public Overrides Function DoInsert() As Boolean
        MyBase.DoInsert()
        DoInsert = False
        Dim cmd As New ADODB.Command
        Dim sb As New StringBuilder
        sb.AppendLine(" Insert Into ")
        sb.AppendLine(" WysBorrowLend ")
        sb.AppendLine(" (")
        sb.AppendLine(" LendBorrowUserName ")
        sb.AppendLine(" ,Count ")
        sb.AppendLine(" ,DateTime ")
        sb.AppendLine(" ,OPTIME ")
        sb.AppendLine(" ,MEMO ")
        sb.AppendLine(" ,UserName ")
        sb.AppendLine(" ,Status")
        sb.AppendLine(" ,TYPE ")
        sb.AppendLine(" ,id ")
        sb.AppendLine(" ) ")
        sb.AppendLine(" Values ")
        sb.AppendLine(" ( ")
        sb.AppendLine(" ? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,0 ")
        If rad借给被人钱.Checked Then
            sb.AppendLine(" ,1 ")
        Else
            sb.AppendLine(" ,3 ")
        End If
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ) ")
        Dim pa As ADODB.Parameter
        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adChar, ADODB.ParameterDirectionEnum.adParamInput, 36, Me.txtBorrowUserName.Tag.ToString)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDecimal, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Value = numUpDown.Value
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, dt1.Value.ToString)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, DateTime.Now.ToString())
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 100, Me.txtMemo.Text.Trim)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 36, Me.txtUsername.Tag.ToString.Trim)
        cmd.Parameters.Append(pa)

        Dim strLastId As String = System.Guid.NewGuid.ToString("D")
        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adChar, ADODB.ParameterDirectionEnum.adParamInput, 36, strLastId)
        cmd.Parameters.Append(pa)

        cmd.CommandText = sb.ToString()
        cmd.CommandType = ADODB.CommandTypeEnum.adCmdText
        If ct.ExcCommand(cmd) Then
            '更新关联数据
            InsertCrashInOut(Me.txt支出关联1.Tag.ToString, Me.NM支出关联金额1.Value, Me.txtBorrowUserName.Tag.ToString, strLastId)
            InsertCrashInOut(Me.txt支出关联2.Tag.ToString, Me.NM支出关联金额2.Value, Me.txtBorrowUserName.Tag.ToString, strLastId)

            If rad借给被人钱.Checked = True Then
                '插入金额 2022-11-20
                Account_Detail.Update还入_加(ct, dt1.Value.ToString("yyyy-MM-dd"), Me.txtUsername.Tag.ToString.Trim, numUpDown.Value)

                Crash_Detail.Update还入_加(ct, dt1.Value.ToString("yyyy-MM-dd"), Me.txt支出关联1.Tag.ToString, Me.NM支出关联金额1.Value)
                Crash_Detail.Update还入_加(ct, dt1.Value.ToString("yyyy-MM-dd"), Me.txt支出关联2.Tag.ToString, Me.NM支出关联金额2.Value)
            Else
                '插入金额 2022-11-20
                Account_Detail.Update还出_加(ct, dt1.Value.ToString("yyyy-MM-dd"), Me.txtUsername.Tag.ToString.Trim, numUpDown.Value)
                Crash_Detail.Update还出_加(ct, dt1.Value.ToString("yyyy-MM-dd"), Me.txt支出关联1.Tag.ToString, Me.NM支出关联金额1.Value)
                Crash_Detail.Update还出_加(ct, dt1.Value.ToString("yyyy-MM-dd"), Me.txt支出关联2.Tag.ToString, Me.NM支出关联金额2.Value)
            End If

            DoInsert = True
            ShowMessageBox("插入成功")

            Me.bAdd = True '表示成功插入数据能刷新界面 
        Else
            ShowMessageBox("插入失败")
            DoInsert = False
            Me.bAdd = False  '表示成功插入数据能刷新界面 
        End If

        ClearForm()

    End Function

#End Region

#Region "清除窗体"
    Public Sub ClearForm()

        Me.numUpDown.Value = 0
        Me.dt1.Value = Now
        Me.txtMemo.Text = ""
        Me.txtUsername.Text = ""
        Me.txtUsername.Tag = ""

        Me.txtBorrowUserName.Text = ""
        Me.txtBorrowUserName.Tag = ""

        Me.txt支出关联1.Text = ""
        Me.txt支出关联1.Tag = ""

        Me.txt支出关联2.Text = ""
        Me.txt支出关联2.Tag = ""

        NM支出关联金额1.Value = 0
        NM支出关联金额2.Value = 0
    End Sub
#End Region

#Region "加载事件"

    Public Overrides Sub InitAdd()
        ClearForm()
    End Sub

    Private Sub InputAdd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
#End Region

#Region "更新模式"

    Private Sub UpdateCrashInOut()
        Dim sb As New StringBuilder
        Dim dt As New DataTable
        Dim rs As ADODB.Recordset = ct.GetTable("SELECT * from CrashInOut  where LendId='" + CStr(Me.参数列表("ID")) + "'")
        dt = ct.FillTable(rs)
        If dt.Rows.Count > 0 Then
            Dim cmdSub As ADODB.Command = New ADODB.Command
            cmdSub.CommandType = ADODB.CommandTypeEnum.adCmdText
            For Each dr As DataRow In dt.Rows
                If CStr(dr("CrashStatusID")) <> "" AndAlso CDec(dr("CrashStatusCount")) > 0 Then
                    sb.Length = 0
                    If rad借给被人钱.Checked Then
                        sb.AppendLine("Update CrashStatus set Count=Count -" & CStr(dr("CrashStatusCount")) & " Where id='" & CStr(dr("CrashStatusID")) + "'")
                    Else
                        sb.AppendLine("Update CrashStatus set Count=Count +" & CStr(dr("CrashStatusCount")) & " Where id='" & CStr(dr("CrashStatusID")) + "'")
                    End If
                    cmdSub.CommandText = sb.ToString
                    ct.ExcCommand(cmdSub)
                    UpdateCrashOptime(ct, CStr(dr("CrashStatusID")))

                    If rad借给被人钱.Checked = True Then
                        '插入金额 2022-11-20
                        Account_Detail.Update还入_减(ct, CDate(dr("DATETIME")).ToString("yyyy-MM-dd"), dr("USERNAME").ToString, CDec(dr("CrashStatusCount")))
                        Crash_Detail.Update还入_减(ct, CDate(dr("DATETIME")).ToString("yyyy-MM-dd"), dr("CrashStatusID").ToString, CDec(dr("CrashStatusCount")))
                    Else
                        '插入金额 2022-11-20
                        Account_Detail.Update还出_减(ct, CDate(dr("DATETIME")).ToString("yyyy-MM-dd"), dr("USERNAME").ToString, CDec(dr("CrashStatusCount")))
                        Crash_Detail.Update还出_减(ct, CDate(dr("DATETIME")).ToString("yyyy-MM-dd"), dr("CrashStatusID").ToString, CDec(dr("CrashStatusCount")))
                    End If
                End If
            Next
        End If
        ct.GetTable("DELETE from CrashInOut  where LendId='" + CStr(Me.参数列表("ID")) + "'")
    End Sub

    Public Overrides Function DoUpdate() As Boolean
        MyBase.DoUpdate()
        DoUpdate = False

        Dim sb As New StringBuilder

        UpdateCrashInOut()

        Dim cmd As New ADODB.Command
        sb.Length = 0
        sb.AppendLine(" UPDATE ")
        sb.AppendLine(" WysBorrowLend ")
        sb.AppendLine(" SET ")
        sb.AppendLine("  LendBorrowUserName=? ")
        sb.AppendLine(" ,Count=? ")
        sb.AppendLine(" ,DateTime=? ")
        sb.AppendLine(" ,Memo=? ")
        sb.AppendLine(" ,UserName=?  ")
        sb.AppendLine(" ,OPTIME=? ")
        sb.AppendLine("  WHERE  ")
        sb.AppendLine("  ID=? ")

        Dim pa As ADODB.Parameter
        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adChar, ADODB.ParameterDirectionEnum.adParamInput, 36, Me.txtBorrowUserName.Tag.ToString)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDecimal, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Value = numUpDown.Value
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, dt1.Value.ToString)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 100, Me.txtMemo.Text.Trim)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 36, Me.txtUsername.Tag.ToString.Trim)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, DateTime.Now.ToString())
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 36, CStr(Me.参数列表("ID")))
        cmd.Parameters.Append(pa)

        cmd.CommandText = sb.ToString()
        If (ct.ExcCommand(cmd)) Then
            '更新关联数据
            InsertCrashInOut(Me.txt支出关联1.Tag.ToString, Me.NM支出关联金额1.Value, Me.txtBorrowUserName.Tag.ToString, CStr(Me.参数列表("ID")))
            InsertCrashInOut(Me.txt支出关联2.Tag.ToString, Me.NM支出关联金额2.Value, Me.txtBorrowUserName.Tag.ToString, CStr(Me.参数列表("ID")))

            If rad借给被人钱.Checked = True Then
                '插入金额 2022-11-20
                Account_Detail.Update还入_加(ct, dt1.Value.ToString("yyyy-MM-dd"), Me.txtUsername.Tag.ToString.Trim, numUpDown.Value)

                Crash_Detail.Update还入_加(ct, dt1.Value.ToString("yyyy-MM-dd"), Me.txt支出关联1.Tag.ToString, Me.NM支出关联金额1.Value)
                Crash_Detail.Update还入_加(ct, dt1.Value.ToString("yyyy-MM-dd"), Me.txt支出关联2.Tag.ToString, Me.NM支出关联金额2.Value)
            Else
                '插入金额 2022-11-20
                Account_Detail.Update还出_加(ct, dt1.Value.ToString("yyyy-MM-dd"), Me.txtUsername.Tag.ToString.Trim, numUpDown.Value)
                Crash_Detail.Update还出_加(ct, dt1.Value.ToString("yyyy-MM-dd"), Me.txt支出关联1.Tag.ToString, Me.NM支出关联金额1.Value)
                Crash_Detail.Update还出_加(ct, dt1.Value.ToString("yyyy-MM-dd"), Me.txt支出关联2.Tag.ToString, Me.NM支出关联金额2.Value)
            End If

            ShowMessageBox("更新成功")
            bAdd = True
            Me.Close()
        Else
            ShowMessageBox("更新失败")
        End If
        DoUpdate = True
    End Function

    Public Overrides Function DoUpdateCheck() As Boolean
        MyBase.DoUpdateCheck()
        DoUpdateCheck = False
        If Me.txtBorrowUserName.Text.Trim = String.Empty Then
            ShowMessageBox("借款用户不能为空")
            Me.btnUsername.Focus()
            Exit Function
        End If

        If numUpDown.Value <= 0 Then
            ShowMessageBox("借款不能小于0")
            Me.numUpDown.Focus()
            Exit Function
        End If

        'If Me.chk是否启用关联更新.Checked Then
        '    If numUpDown.Value <> Me.NM支出关联金额1.Value + Me.NM支出关联金额2.Value Then
        '        ShowMessageBox("关联金额必须与还入金额相等")
        '        Me.NM支出关联金额1.Focus()
        '        Exit Function
        '    End If
        'End If

        If chk是否启用关联更新.Checked Then
            If numUpDown.Value <> Me.NM支出关联金额1.Value + Me.NM支出关联金额2.Value Then
                If ShowMessageResult("关联金额与还款金额不相等,是否继续？") = Windows.Forms.DialogResult.No Then
                    Me.NM支出关联金额1.Focus()
                    Exit Function
                End If
            End If
        End If

        If Me.txtUsername.Text.Trim = String.Empty Then
            ShowMessageBox("操作用户不能为空")
            Me.btnUsername.Focus()
            Exit Function
        End If

        DoUpdateCheck = True
    End Function

    Public Overrides Function DoUpdateExist() As Boolean
        DoUpdateExist = False
        MyBase.DoUpdateExist()
        If CStr(Me.参数列表("ID")).Trim = "" Then
            ShowMessageBox("没有选择任何记录")
            Exit Function
        End If
        DoUpdateExist = True
    End Function

    Public Overrides Sub InitUpdate()
        MyBase.InitUpdate()
        Me.Text = "借出更新"
        ClearForm()
        Me.btnLendUsername.Visible = False
        NumericUpDown1.Visible = False
        Dim rs As ADODB.Recordset = Nothing
        Dim sb As New StringBuilder

        sb.AppendLine(" select WysBorrowLend.*")
        sb.AppendLine(" ,WysInputUser1.UserName as UserName1")
        sb.AppendLine(" ,WysInputUser2.UserName as UserName2")
        sb.AppendLine("  from WysBorrowLend ")
        sb.AppendLine("INNER join WysBorrowLend WysBorrowLend1  on WysBorrowLend.LendBorrowUserName =WysBorrowLend1.ID ")
        sb.AppendLine("left join WysInputUser WysInputUser1 on WysInputUser1.id =WysBorrowLend1.LendBorrowUserName ")
        sb.AppendLine("left join WysInputUser WysInputUser2 on WysInputUser2.id =WysBorrowLend.username ")
        sb.AppendLine(" WHERE WysBorrowLend.type=" + CStr(Me.参数列表("TYPE")) + " and WysBorrowLend.id='" + CStr(Me.参数列表("ID")) + "'")

        rs = ct.GetTable(sb.ToString)

        If rs IsNot Nothing Then
            If rs.EOF = False Then

                Me.dt1.Text = CMainModule.CmnToStr(rs.Fields("DateTime").Value)
                Me.txtMemo.Text = CMainModule.CmnToStr(rs.Fields("Memo").Value)
                Me.numUpDown.Text = CDec(rs.Fields("Count").Value)

                Me.txtUsername.Text = UTIL.CUtil.ConvertText(rs.Fields("UserName2").Value)
                Me.txtUsername.Tag = UTIL.CUtil.ConvertText(rs.Fields("UserName").Value)

                Me.txtBorrowUserName.Text = UTIL.CUtil.ConvertText(rs.Fields("UserName1").Value)
                Me.txtBorrowUserName.Tag = UTIL.CUtil.ConvertText(rs.Fields("LendBorrowUserName").Value)

                If CDec(rs.Fields("type").Value) = 1 Then
                    Me.rad借给被人钱.Checked = True
                    Me.rad向别人借钱.Checked = False
                Else
                    Me.rad借给被人钱.Checked = False
                    Me.rad向别人借钱.Checked = True
                End If

                Dim dt As New DataTable
                Dim rs1 As ADODB.Recordset = ct.GetTable("SELECT CrashInOut.*, CrashStatus.TITLE from CrashInOut left join CrashStatus on CrashStatus.ID=CrashInOut.CrashStatusID where LendId='" + CStr(Me.参数列表("ID")) + "'")
                dt = ct.FillTable(rs1)

                If dt.Rows.Count > 0 Then
                    Me.txt支出关联1.Text = UTIL.CUtil.ConvertText(dt.Rows(0)("Title"))
                    Me.txt支出关联1.Tag = UTIL.CUtil.ConvertText(dt.Rows(0)("CrashStatusID"))
                    Me.NM支出关联金额1.Value = CDec(dt.Rows(0)("CrashStatusCount"))
                End If

                If dt.Rows.Count > 1 Then
                    Me.txt支出关联2.Text = UTIL.CUtil.ConvertText(dt.Rows(1)("Title"))
                    Me.txt支出关联2.Tag = UTIL.CUtil.ConvertText(dt.Rows(1)("CrashStatusID"))
                    Me.NM支出关联金额2.Value = CDec(dt.Rows(1)("CrashStatusCount"))
                End If

                GroupBox4.Enabled = False
            End If
            rs.Close()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs)
        End If
        rs = Nothing
    End Sub

#End Region

#Region "更新模式"

    Private Sub InsertCrashInOut(ByVal 状态ID As String, ByVal 支出金额 As Decimal, ByVal 支出ID As String, ByVal id As String)

        If 状态ID <> "" AndAlso 支出金额 > 0 Then
            Dim sb As New StringBuilder
            Dim cmd As ADODB.Command = New ADODB.Command

            sb.Length = 0
            If rad借给被人钱.Checked Then
                sb.AppendLine("Update CrashStatus set Count=Count +" & 支出金额.ToString & " Where id='" & 状态ID.ToString & "'")
            Else
                sb.AppendLine("Update CrashStatus set Count=Count -" & 支出金额.ToString & " Where id='" & 状态ID.ToString & "'")
            End If
            cmd.CommandText = sb.ToString
            ct.ExcCommand(cmd)
            UpdateCrashOptime(ct, 状态ID.ToString)

            sb.Length = 0
            Dim pa As ADODB.Parameter
            pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adChar, ADODB.ParameterDirectionEnum.adParamInput, 36, System.Guid.NewGuid.ToString("D"))
            cmd.Parameters.Append(pa)

            pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adChar, ADODB.ParameterDirectionEnum.adParamInput, 36, 支出ID)
            cmd.Parameters.Append(pa)

            pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adChar, ADODB.ParameterDirectionEnum.adParamInput, 36, 状态ID)
            cmd.Parameters.Append(pa)

            pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDecimal, ADODB.ParameterDirectionEnum.adParamInput)
            pa.Value = 支出金额
            cmd.Parameters.Append(pa)

            pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, DateTime.Now.ToString())
            cmd.Parameters.Append(pa)

            pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 36, Me.txtUsername.Tag.ToString.Trim)
            cmd.Parameters.Append(pa)

            pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 100, Me.txtMemo.Text.Trim)
            cmd.Parameters.Append(pa)

            pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, dt1.Value.ToString)
            cmd.Parameters.Append(pa)

            pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adChar, ADODB.ParameterDirectionEnum.adParamInput, 36, id)
            cmd.Parameters.Append(pa)

            sb.AppendLine("Insert into CrashInOut values(?,?,?,?,?,4,?,?,?,?)")

            cmd.CommandText = sb.ToString
            ct.ExcCommand(cmd)
        End If

    End Sub

#End Region

    Private Sub btnUsername_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUsername.Click
        Using frm As Wys.Control.Vb.ListForm = New Wys.Control.Vb.ListForm
            frm.Text = "借款人一览"
            frm.strName = Me.strName
            frm.iType = Me.iType
            frm.SQL = "select ID,UserName as 名称,memo as 备注 from WysInputUser where type=0 order by 名称"
            Dim 显示列 As New StringCollection
            显示列.Add("名称")
            显示列.Add("备注")
            frm.Width = 250
            frm.Height = 200
            frm.显示列 = 显示列
            frm.ShowDialog()
            If frm.返回值.Count > 0 Then
                txtUsername.Tag = CStr(frm.返回值(0))
                txtUsername.Text = CStr(frm.返回值(1))
            End If
        End Using
    End Sub

    Private Sub btn支出关联_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn支出关联1.Click, btn支出关联2.Click
        Using frm As Wys.Control.Vb.ListForm = New Wys.Control.Vb.ListForm
            frm.Text = "账户一览"
            frm.strName = Me.strName
            frm.iType = Me.iType
            frm.SQL = "select ID,TITLE as 名称,Count as 金额,MEMO AS 备注 from CrashStatus order by 名称"
            Dim 显示列 As New StringCollection
            显示列.Add("名称")
            显示列.Add("金额")
            显示列.Add("备注")
            frm.Width = 300
            frm.Height = 300
            frm.显示列 = 显示列
            frm.ShowDialog()
            If frm.返回值.Count > 0 Then
                If CType(sender, Button) Is Me.btn支出关联1 Then
                    txt支出关联1.Tag = CStr(frm.返回值(0))
                    txt支出关联1.Text = CStr(frm.返回值(1))
                    NM支出关联金额1.Value = numUpDown.Value - NM支出关联金额2.Value
                ElseIf CType(sender, Button) Is Me.btn支出关联2 Then
                    txt支出关联2.Tag = CStr(frm.返回值(0))
                    txt支出关联2.Text = CStr(frm.返回值(1))
                    NM支出关联金额2.Value = numUpDown.Value - NM支出关联金额1.Value
                End If
            End If
        End Using
    End Sub

    Private Sub NM支出关联金额1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles NM支出关联金额1.Validating, NM支出关联金额2.Validating
        If CType(sender, NumericUpDown) Is Me.NM支出关联金额1 Then
            If Me.NM支出关联金额1.Value = 0 Then
                Me.txt支出关联1.Tag = ""
                Me.txt支出关联1.Text = ""
            End If
            If Me.txt支出关联1.Tag.ToString = "" Then
                Me.NM支出关联金额1.Value = 0
            End If
        ElseIf CType(sender, NumericUpDown) Is Me.NM支出关联金额2 Then
            If Me.NM支出关联金额2.Value = 0 Then
                Me.txt支出关联2.Tag = ""
                Me.txt支出关联2.Text = ""
            End If
            If Me.txt支出关联2.Tag.ToString = "" Then
                Me.NM支出关联金额2.Value = 0
            End If
        End If
    End Sub

    Private Sub chk是否启用关联更新_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chk是否启用关联更新.Click
        If Not Me.chk是否启用关联更新.Checked Then
            Me.NM支出关联金额1.Enabled = False
            Me.btn支出关联1.Enabled = False
            Me.txt支出关联1.Text = ""
            Me.txt支出关联1.Tag = ""
            Me.NM支出关联金额1.Value = 0

            Me.NM支出关联金额2.Enabled = False
            Me.btn支出关联2.Enabled = False
            Me.txt支出关联2.Text = ""
            Me.txt支出关联2.Tag = ""
            Me.NM支出关联金额2.Value = 0

        Else
            Me.NM支出关联金额1.Enabled = True
            Me.btn支出关联1.Enabled = True

            Me.NM支出关联金额1.Enabled = True
            Me.btn支出关联2.Enabled = True

        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLendUsername.Click
        Dim sb As New StringBuilder
        sb.AppendLine("select")
        sb.AppendLine(" WysBorrowLend.id ")
        sb.AppendLine(",DATE(WysBorrowLend.DateTime) as 借款时间")
        sb.AppendLine(",WysInputUser.Username as 借款人")
        sb.AppendLine(",WysBorrowLend.Count as 借款金额")
        sb.AppendLine(",ifnull((select SUM(CrashStatusCount) from CrashInOut where InOutID=WysBorrowLend.ID  AND CrashInOut.type=4 group by CrashInOut.InOutID),0) as 还入金额")
        sb.AppendLine(",WysInputUser1.Username as 借出人")
        sb.AppendLine("from WysBorrowLend")
        sb.AppendLine("left join WysInputUser on WysInputUser.id =WysBorrowLend.LendBorrowUserName")
        sb.AppendLine("left join WysInputUser WysInputUser1 on WysInputUser1.id =WysBorrowLend.UserName")
        If rad借给被人钱.Checked Then
            sb.AppendLine("WHERE WysBorrowLend.status<>1 and WysBorrowLend.type = 0")
        Else
            sb.AppendLine("WHERE WysBorrowLend.status<>1 and WysBorrowLend.type = 2")
        End If
        sb.AppendLine("order by 借款时间")
        Using frm As Wys.Control.Vb.ListForm = New Wys.Control.Vb.ListForm
            frm.Text = "借款一览"
            frm.strName = Me.strName
            frm.iType = Me.iType
            frm.SQL = sb.ToString
            Dim 显示列 As New StringCollection
            显示列.Add("借款时间")
            显示列.Add("借款人")
            显示列.Add("借款金额")
            显示列.Add("还入金额")
            显示列.Add("借出人")
            frm.Width = 450
            frm.Height = 200
            frm.显示列 = 显示列
            frm.ShowDialog()
            If frm.返回值.Count > 0 Then
                txtBorrowUserName.Tag = CStr(frm.返回值(0))
                txtBorrowUserName.Text = CStr(frm.返回值(1)) & "/" & CStr(frm.返回值(2))
                NumericUpDown1.Value = CDec(frm.返回值(3)) - CDec(frm.返回值(4))
            End If
        End Using
    End Sub

    Private Sub rad借给被人钱_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rad向别人借钱.Click, rad借给被人钱.Click
        If Me.模式 = 模式型.添加 Then
            txtBorrowUserName.Tag = ""
            txtBorrowUserName.Text = ""
            NumericUpDown1.Value = 0
        End If
    End Sub
End Class